package com.mdd.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdd.admin.service.IMediationRecordService;
import com.mdd.admin.validate.MediationRecordSearchValidate;
import com.mdd.admin.validate.commons.PageValidate;
import com.mdd.admin.vo.MediationRecordDetailVo;
import com.mdd.admin.vo.MediationRecordListedVo;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.MediationRecord;
import com.mdd.common.mapper.MediationRecordMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.LinkedList;
import java.util.List;

/**
 * 调解记录实现类
 * @author LikeAdmin
 */
@Service
public class MediationRecordServiceImpl implements IMediationRecordService {
        
    @Resource
    MediationRecordMapper mediationRecordMapper;

    /**
     * 调解记录列表
     *
     * @author LikeAdmin
     * @param pageValidate 分页参数
     * @param searchValidate 搜索参数
     * @return PageResult<MediationRecordListedVo>
     */
    @Override
    public PageResult<MediationRecordListedVo> list(PageValidate pageValidate, MediationRecordSearchValidate searchValidate) {
        Integer page  = pageValidate.getPageNo();
        Integer limit = pageValidate.getPageSize();

        QueryWrapper<MediationRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("mediation_number");

        mediationRecordMapper.setSearch(queryWrapper, searchValidate, new String[]{
            "like:nameCase@name_case:str",
            "=:caseNumber@case_number:str",
            "=:courtClerk@court_clerk:str",
            "=:courtId@court_id:long",
            "=:caseType@case_type:long",
            "=:mediationResult@mediation_result:long",
            "=:receptionTime@reception_time:str",
            "=:completionTime@completion_time:str",
            "=:video:str",
            "=:mediationDocument@mediation_document:str",
            "=:caseJudgment@case_judgment:str",
            "=:customerNumber@customer_number:long",
            "=:judicialConfirmation@judicial_confirmation:str",
            "=:remark:str",
            "=:founder:str",
            "=:creationTime@creation_time:str",
            "=:modifier:str",
            "=:modificationTime@modification_time:str",
            "=:status:long",
            "=:isFirstMediation@is_first_mediation:long",
            "=:isJudicialTreatment@is_judicial_treatment:long",
        });

        IPage<MediationRecord> iPage = mediationRecordMapper.selectPage(new Page<>(page, limit), queryWrapper);

        List<MediationRecordListedVo> list = new LinkedList<>();
        for(MediationRecord item : iPage.getRecords()) {
            MediationRecordListedVo vo = new MediationRecordListedVo();
            BeanUtils.copyProperties(item, vo);
            if (vo.getReceptionTime() != null) {
                vo.setReceptionTimeStr(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(vo.getReceptionTime()));
            }
            if (vo.getCompletionTime() != null) {
                vo.setCompletionTimeStr(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(vo.getCompletionTime()));
            }
            list.add(vo);
        }

        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    /**
     * 调解记录详情
     *
     * @author LikeAdmin
     * @param id 主键参数
     * @return MediationRecord
     */
    @Override
    public MediationRecordDetailVo detail(Integer id) {
        MediationRecord model = mediationRecordMapper.selectOne(
                new QueryWrapper<MediationRecord>()
                    .eq("mediation_number", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在");

        MediationRecordDetailVo vo = new MediationRecordDetailVo();
        BeanUtils.copyProperties(model, vo);
        if (model.getIsFirstMediation() != null) {
            vo.setIsFirstMediation(model.getIsFirstMediation().toString());
        }
        if (model.getIsJudicialTreatment() != null) {
            vo.setIsJudicialTreatment(model.getIsJudicialTreatment().toString());
        }
        return vo;
    }

}
